<?php
/*
  Part of the Adwit plugin for WordPress
  http://www.etineria.com/
 */

if (!is_admin()) {
    return;
}

if (isset($_POST['adwit_action'])) {
    switch ($_POST['adwit_action']) {
        case 'adwit_add_banner':

            $adwit_data = adwit_options();
            if (empty($adwit_data['adwit_express_key'])) {
                //@TODO, Log message
                return;
            }

            $post_v = $_POST['pos_v'];

            switch ($_POST['pos_v']) {
                case 'content_top':
                    $post_v = 'top';
                    break;
                case 'content_middle':
                    $post_v = 'middle';
                    break;
                case 'content_bottom':
                    $post_v = 'bottom';
                    break;
            }

            $file_name = stripslashes($_POST['post_type'] . '_' . $post_v . '_' . $_POST['pos_h']);
            $bsizes = explode('x', substr($_POST['bsize'], 3));

            $bann_list = _get_bann_list();

            $url_data = array();
            $url_data['page_type'] = $_POST['post_type'];
            $url_data['pos_v'] = $post_v;
            $url_data['pos_h'] = $_POST['pos_h'];
            $url_data['size_id'] = $bann_list[substr($_POST['bsize'], 3)]['id'];
            $url_data['status'] = 'on';
            $url_data['publisher_id'] = $adwit_data['adwit_express_publisher_id'];

            //Get zone key
            $url_express_create_zone = ADWIT_HOME_URL . '/' . adwit_generate_token('campaign_ads/zone_add', $url_data, $adwit_data['adwit_express_key']);
            $get_zone_key = wp_remote_fopen($url_express_create_zone);

            //Add new banner info into adwit_options (wp)
            $ad = array();
            $ad['page_type'] = $_POST['post_type'];
            $ad['pos_v'] = $post_v;
            $ad['pos_h'] = $_POST['pos_h'];
            $ad['width'] = $bsizes[0];
            $ad['height'] = $bsizes[1];


            $ad['bsize'] = $_POST['bsize'];
            $ad['key'] = $get_zone_key; //zone key
            $ad['img_name'] = substr($_POST['bsize'], 3) . '.png';
            $adwit_data['ads'][$file_name] = $ad;
            update_option('adwit_options', $adwit_data);
            break;

        case 'adwit_remove_banner':
            $pos = str_split($_POST['pos']);
            $file_name = $_POST['post_type'];

            if (is_array($pos) && !empty($pos)) {
                switch ($pos[0]) {
                    case 'h':
                        $file_name .= '_head';
                        $post_v = 'head';
                        break;
                    case 'c':
                        switch ($pos[1]) {
                            case 't':
                                $file_name .= '_top';
                                $post_v = 'top';
                                break;

                            case 'm':
                                $file_name .= '_middle';
                                $post_v = 'middle';
                                break;
                            case 'b':
                                $file_name .= '_bottom';
                                $post_v = 'bottom';
                                break;
                        }
                        break;
                }

                switch ($pos[2]) {
                    case 1:
                        $file_name .= '_left';
                        $post_h = 'left';
                        break;
                    case 2:
                        $file_name .= '_center';
                        $post_h = 'center';
                        break;
                    case 3:
                        $file_name .= '_right';
                        $post_h = 'right';
                        break;
                }

                if (!empty($file_name)) {
                    $adwit_data = adwit_options();

                    $url_data = array();
                    $url_data['page_type'] = $_POST['post_type'];
                    $url_data['pos_v'] = $post_v;
                    $url_data['pos_h'] = $post_h;
                    $url_data['status'] = 'off';
                    $url_data['publisher_id'] = $awt_options['adwit_express_publisher_id'];

                    //Get zone key
                    $url_express_update_zone = ADWIT_HOME_URL . '/' . adwit_generate_token('campaign_ads/zone_add', $url_data, $adwit_data['adwit_express_key']);
                    wp_remote_fopen($url_express_update_zone);

                    unset($adwit_data['ads'][$file_name]);
                    update_option('adwit_options', $adwit_data);
                }
            }

            break;

        case 'delete_adwit_action':
            //delete_option('adwit_options');
            break;
    }
}

function adwit_generate_token($path, $data, $private_key) {
    ksort($data);
    $params_line = '';
    foreach ($data as $key => $value) {
        if ($params_line != '') {
            $params_line .= '&';
        }
        $params_line .= "$key=$value";
    }
    $params_line_key = $params_line . '&key=' . $private_key;
    $token = sha1("$path?$params_line_key");
    return "$path?$params_line&sign=$token";

    /*
      $data = array();
      $data['page_type'] = 'search';
      $data['pos_v'] = 'head';
      $data['pos_h'] = 'left';
      $data['size_id'] = '4';
      $data['status'] = 'on';
      $data['publisher_id'] = '12345';
      //ex: generate_token('campaign_ads/zone_add', $data, 'adwit_express_key');
     */
}

function adwit_find_or_create_customer() {
    $adwit_data = adwit_options();

    if ($adwit_data && isset($adwit_data['adwit_express_key']) && !empty($adwit_data['adwit_express_key']) && $adwit_data['adwit_express_ads_enabled']==1) {
        return false;
    }

    global $current_user;
    get_currentuserinfo();

    $wp_url = _get_wp_url();
    $adwit_wp_email = $current_user->user_email;
    $adwit_regie_id = 5;
    $adwit_wp_url = $wp_url;
    $adwit_wp_lang = 'EN';

    $url_express_create_account = ADWIT_HOME_URL . "/campaign_ads/user_add?email=" . $adwit_wp_email . "&regie_id=" . $adwit_regie_id . "&url=" . $adwit_wp_url . "&lang=" . $adwit_wp_lang;
    $get_account = wp_remote_fopen($url_express_create_account);
    //$get_account = $adwit_wp_email . ';873f92043b2a3866daae05579aadf1eadd4288bf;5865';

    $cust_arr = explode(';', $get_account);

    if (!empty($get_account) && is_array($cust_arr) && !empty($cust_arr[1])) {
        $adwit_data['adwit_express_email'] = $cust_arr[0];
        $adwit_data['adwit_express_key'] = $cust_arr[1];
        $adwit_data['adwit_express_publisher_id'] = $cust_arr[2];
    }

    update_option('adwit_options', $adwit_data);
    return true;
}

function _get_wp_url($getfull=false) {
    $s = empty($_SERVER ["HTTPS"]) ? '' : ($_SERVER ["HTTPS"] == "on") ? "s" : "";

    $protocol = strtolower($_SERVER ["SERVER_PROTOCOL"]);
    $protocol = substr($protocol, 0, strpos($protocol, "/"));
    $protocol .= $s;

    $port = ($_SERVER ["SERVER_PORT"] == "80") ? "" : (":" . $_SERVER ["SERVER_PORT"]);

    if ($getfull == true) {
        $res = $protocol . "://" . $_SERVER ['SERVER_NAME'] . $port . $_SERVER ['REQUEST_URI'];
    } else {
        $res = $protocol . "://" . $_SERVER ['SERVER_NAME'] . $port;
    }

    return $res;
}

function adwit_check_ads() {
    $adwit_data = adwit_options();
    $count_ads = _count_adwit_ads();
    $adwit_data['adwit_express_ads_enabled'] = 0;
    if ($count_ads >= 1) {
        $adwit_data['adwit_express_ads_enabled'] = 1;
    }

    update_option('adwit_options', $adwit_data);
    return $count_ads;
}

function _count_adwit_ads() {
    $adwit_data = adwit_options();
    $res = count($adwit_data['ads']);
    return $res;
}

function _get_bann_list() {
    $res = array();
    $res['468x60'] = array('id' => 2, 'class' => "adwit_banner", 'width' => "117px", 'height' => "17px", 'bgcolor' => " #ffcfa0");
    $res['728x90'] = array('id' => 1, 'class' => "adwit_banner", 'width' => "182px", 'height' => "22px", 'bgcolor' => " #ffaf70");
    $res['160x600'] = array('id' => 5, 'class' => "adwit_banner", 'width' => "40px", 'height' => "150px", 'bgcolor' => " #ff8f40");
    $res['120x600'] = array('id' => 4, 'class' => "adwit_banner", 'width' => "30px", 'height' => "150px", 'bgcolor' => " #ff9f90");
    $res['200x200'] = array('id' => 8, 'class' => "adwit_banner", 'width' => "50px", 'height' => "50px", 'bgcolor' => " #ffcf70");
    $res['250x250'] = array('id' => 7, 'class' => "adwit_banner", 'width' => "62px", 'height' => "62px", 'bgcolor' => " #ffaf40");
    $res['300x250'] = array('id' => 6, 'class' => "adwit_banner", 'width' => "75px", 'height' => "62px", 'bgcolor' => " #ff8fa0");
    $res['120x240'] = array('id' => 10, 'class' => "adwit_banner", 'width' => "30px", 'height' => "60px", 'bgcolor' => " #ff6f70");
    $res['336x280'] = array('id' => 12, 'class' => "adwit_banner", 'width' => "84px", 'height' => "70px", 'bgcolor' => " #ffcf40");
    $res['180x150'] = array('id' => 11, 'class' => "adwit_banner", 'width' => "45px", 'height' => "37px", 'bgcolor' => " #ffafa0");
    $res['125x125'] = array('id' => 9, 'class' => "adwit_banner", 'width' => "31px", 'height' => "31px", 'bgcolor' => " #ff8f70");
    $res['234x60'] = array('id' => 3, 'class' => "adwit_banner", 'width' => "58px", 'height' => "17px", 'bgcolor' => " #ff6f40");

    return $res;
}

function _get_date_now_adwit_format() {
    return date('Y-m-d') . '_' . date('h') . 'h' . date('i');
}
